package com.jackrain.nea.core.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

@Mapper
@Component
public interface MapperShortcut {

    static class SqlProvider {
        public String update(Map<String, Object> para) {
            final JSONObject model = (JSONObject) para.get("model");
            final String tableName = (String) para.get("tableName");
            String str = new SQL() {
                {
                    UPDATE(tableName);
                    for (String key : model.keySet()) {
                        SET(key + "= #{model." + key + "}");
                    }
                    WHERE("ID = #{model.ID}");
                }
            }.toString();
            return str;
        }

        public String insert(Map<String, Object> para) {
            final JSONObject model = (JSONObject) para.get("model");
            final String tableName = (String) para.get("tableName");
            return new SQL() {
                {
                    Set<String> keySet = model.keySet();
                    INSERT_INTO(tableName);
                    for (int i = 0; i < model.size(); i++) {
                        String key = (String) keySet.toArray()[i];

                        VALUES(key, "#{model." + key + "}");

                    }
                }
            }.toString();
        }

        public String delete(Map<String, Object> para) {
            final long[] ids = (long[]) para.get("ids");
            System.out.println("ids ->" + ids.length);
            final String tableName = (String) para.get("tableName");
            return new SQL() {
                {
                    DELETE_FROM(tableName);
                    StringBuffer buffer = new StringBuffer();
                    buffer.append("ID in (");
                    if (ids.length == 1) {
                        buffer.append(ids[0]);
                    } else {
                        for (long id : ids) {
                            buffer.append(id);
                            buffer.append(",");
                        }
                        buffer.delete(buffer.lastIndexOf(","), buffer.length());
                    }

                    buffer.append(")");
                    WHERE(buffer.toString());
                }
            }.toString();
        }
    }

    @UpdateProvider(type = SqlProvider.class, method = "update")
    int update(@Param("model") JSONObject model, @Param("tableName") String tableName);

    @DeleteProvider(type = SqlProvider.class, method = "delete")
    int delete(@Param("ids") long[] ids, @Param("tableName") String tableName);

    @Delete("DELETE FROM ${tableName} where id= #{id}")
    int deleteOne(@Param("id") long id, @Param("tableName") String tableName);

    @InsertProvider(type = SqlProvider.class, method = "insert")
    int insert(@Param("model") JSONObject model, @Param("tableName") String tableName);

    @Select("select count(1) FROM ${tableName} where ${column} = #{value} limit 1")
    boolean exists(@Param("tableName")String tableName, @Param("column")String column, @Param("value")Object value);

    @Select("select count(1) FROM ${tableName} where id != #{id} and ${column} = #{value} limit 1")
    boolean existsWithoutId(@Param("tableName")String tableName,@Param("id")long id ,@Param("column")String column, @Param("value")Object value);
}
